package com.jiangxiaofei.citycloud.activity.recruit;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.EmptyUtils;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.base.BaseActivity;
import com.jiangxiaofei.citycloud.bean.DoWorkInfoBean;
import com.jiangxiaofei.citycloud.bean.WorkLabelBean;
import com.jiangxiaofei.citycloud.constants.Constants;
import com.jiangxiaofei.citycloud.constants.IntConstants;
import com.jiangxiaofei.citycloud.constants.StringConstants;
import com.jiangxiaofei.citycloud.event.RefreshMessage;
import com.jiangxiaofei.citycloud.mvp.RecruitConstant;
import com.jiangxiaofei.citycloud.mvp.RecruitPersenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import pub.devrel.easypermissions.EasyPermissions;

/**
 *@author JiangXFei
 */

public class RecruitDoWorkInfoactivity extends BaseActivity implements
        RecruitConstant.DoWorkInfoView, EasyPermissions.PermissionCallbacks {

    @InjectView(R.id.worker_tv_title)
    TextView workerTvTitle;
    @InjectView(R.id.do_work_tv_location)
    TextView doWorkTvLocation;
    @InjectView(R.id.do_work_tv_begin_time)
    TextView doWorkTvBeginTime;
    @InjectView(R.id.do_work_tv_end_time)
    TextView doWorkTvEndTime;
    @InjectView(R.id.do_work_tv_work_label)
    TextView doWorkTvWorkLabel;
    @InjectView(R.id.do_work_tv_name)
    TextView doWorkTvName;
    @InjectView(R.id.do_work_tv_phone)
    TextView doWorkTvPhone;
    @InjectView(R.id.do_work_img_call_phone)
    ImageView doWorkImgCallPhone;
    @InjectView(R.id.do_work_btn_pulish)
    Button btnClose;
    private Intent intent;
    private String intentType;
    RecruitConstant.DoWorkInfoPersenter doWorkInfoPersenter;
    private int userId;
    String[] perms = {android.Manifest.permission.CALL_PHONE};

    @Override
    protected void initView(Bundle savedInstanceState) {
        mActivity = this;
        mContext = this;
        setContentView(R.layout.activity_recruit_worker_info_layout);
        ButterKnife.inject(this);
        intent = getIntent();
        initTitle();
        intentType = intent.getStringExtra(StringConstants.INTENTTYPE);
        if (EmptyUtils.isNotEmpty(intentType) && intentType.equals(StringConstants.MEINFO)) {
            userId = Constants.userId;
        } else {
            userId = intent.getIntExtra(StringConstants.USERID, 0);
        }
        doWorkInfoPersenter = new RecruitPersenter(mContext, (RecruitConstant.DoWorkInfoView) mActivity);
        doWorkInfoPersenter.getDoWorkInfo();
        EventBus.getDefault().register(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refresh(RefreshMessage message) {
        doWorkInfoPersenter.getDoWorkInfo();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void setListener() {
        setCTitle("我要干活(自我介绍)");
        setLeftOneBg(R.drawable.ic_back);
        if (EmptyUtils.isNotEmpty(intentType) && intentType.equals(StringConstants.MEINFO)) {
            setRightThreeBg(R.drawable.ic_update);
        }
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        mRightThree.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_rightthree:
                Intent intent = new Intent(mContext, RecruitDoWorkPublishAcitivty.class);
                intent.putExtra(StringConstants.USERID, userId);
                intent.putExtra(StringConstants.DOWORKTYPE, StringConstants.UPDATE);
                startActivity(intent);
                break;
            default:
                break;
        }
    }

    private void CallPhone() {
        Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + doWorkTvPhone.getText().toString()));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    }

    @OnClick({R.id.do_work_img_call_phone, R.id.do_work_btn_pulish})
    public void OnClick(View v) {
        switch (v.getId()) {
            case R.id.do_work_img_call_phone:
                //编辑详情页
                if (EasyPermissions.hasPermissions(mContext, perms)) {
                    CallPhone();
                } else {
                    EasyPermissions.requestPermissions(this, "需要拨打电话权限",
                            IntConstants.CALLPHONECODE, perms);
                }
                break;
            case R.id.do_work_btn_pulish:
                this.finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void setData(Object o) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void message(String str) {

    }

    @Override
    public int getDoWorkInfo() {
        return userId;
    }

    @Override
    public void getDoworkSuccess(Object o) {
        List<DoWorkInfoBean> doWorkInfoList = (List<DoWorkInfoBean>) o;
        if (doWorkInfoList.size() > 0) {
            DoWorkInfoBean infoBean = doWorkInfoList.get(0);
            workerTvTitle.setText(infoBean.getIntroduce());//标题
            doWorkTvPhone.setText(infoBean.getName());//电话号码
            doWorkTvLocation.setText(infoBean.getCoverage());//服务区域
            if (Constants.workLabelList != null && Constants.workLabelList.size() > 0) {
                String[] split = infoBean.getRole().split(",");
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < split.length; i++) {
                    Integer integer = Integer.valueOf(split[i]);
                    for (WorkLabelBean label : Constants.workLabelList) {
                        if (label.getId() == integer) {
                            if (sb.length() > 0) {
                                sb.append(",");
                            }
                            sb.append(label.getName());
                        }
                    }
                }
                doWorkTvWorkLabel.setText(sb.toString());//工作标签
            } else {
                doWorkTvWorkLabel.setText(infoBean.getRole());//工作标签
            }
            doWorkTvName.setText(infoBean.getRealname());//用户名
            String[] split = infoBean.getFreetime().split(",");
            doWorkTvBeginTime.setText(split[0]);//开始时间
            doWorkTvEndTime.setText(split[1]);//结束时间
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        if (requestCode == IntConstants.CALLPHONECODE) {
            CallPhone();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        if (requestCode == IntConstants.CALLPHONECODE) {
            Toast.makeText(mContext, "你需要提供拨号权限", Toast.LENGTH_SHORT).show();
        }
    }
}
